<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\SeaAction;
use App\Admin\Actions\Transfer;
use App\Admin\Extensions\CustomerExcelExporter;
use App\Admin\Grid\Filter\TimestampBetween;
use App\Customer;
use App\Project;
use App\User;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

class CustomerController extends AdminController
{
    protected       $states = [
        'on'  => ['value' => 0, 'text' => '未锁定', 'color' => 'success'],
        'off' => ['value' => 1, 'text' => '锁定', 'color' => 'danger'],
    ];
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '客户管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Customer());


        $grid->column('customer_id', __('顾客id'));
        $grid->column('name', __('名称'));
        $grid->column('is_lock', __('是否锁定'))->switch($this->states);
        $grid->column('deal_status', __('交易状态'));
        $grid->column('mobile', __('手机号'));
        $grid->column('record.content','跟进内容');
        $grid->column('owner_user_id', __('负责人'))->select(User::getSelectOptions());
        $grid->column('create_user_id', __('创建者'))->select(User::getSelectOptions());
        $grid->column('address', __('地址'));
        $grid->column('daofang_time','到访时间')->display(function ($value){
            return  $value ==0 ? '未到访' :date('Y-m-d H:i:s',$value);
        });
        $grid->column('deal_time','成交时间')->display(function ($value){
            return $this->deal_status=='已成交' ? date('Y-m-d H:i:s',$value) : '';
        })->sortable();
        $grid->column('baobei_time','报备时间')->display(function ($value){
            return  $value ==0 ? '未报备' :date('Y-m-d H:i:s',$value);
        });
        $grid->column('create_time', __('创建时间'))->display(function ($value){
            return \Illuminate\Support\Carbon::parse($value)->toDateTimeString();
        })->sortable();
        $grid->column('daoqi_time', __('公海到期时间'))->display(function ($value){
            if ($this->create_time<=strtotime('2020-08-01 00:00:00')){
                return Carbon::parse($this->deal_time)->addDays(60)->toDateTimeString();
            }else{
                return Carbon::parse($this->deal_time)->addDays(20)->toDateTimeString();
            }
        });
        $grid->column('project_id', __('项目'))->select(Project::getSelectOptions());
        $grid->filter(function (Grid\Filter $filter){
            $filter->disableIdFilter();

            $filter->like('mobile','手机号');
            $filter->where(function ($query){
                switch ($this->input) {
                    case 1:
                       $query->where('deal_status','已成交');
                        break;
                    case 2:
                        $query->where('deal_status','未成交');
                        break;
                }
            },'成交状态','deal_status')->select([3 => '全部',1=>'已成交',2=>'未成交']);
            $filter->in('is_sea','公海')->select(['客户','公海']);
            $filter->where(function ($query){
                $query->whereIn('customer_id',function ($query){
                    $query->select('types_id')->from('5kcrm_admin_record')
                        ->where('content','like','%独栋%')
                    ;

                });
            },'跟进记录');
            $filter->where(function ($query){
                switch ($this->input) {
                    case 1:
                        $query->where('daofang_time','>',0);
                        break;
                    case 2:
                        $query->where('daofang_time','=',null);
                        break;
                }
            },'到访状态','daofang')->select([3 => '全部',1=>'已到访',2=>'未到访']);
            $filter->use(new TimestampBetween('deal_time','成交时间'))->datetime();

            $filter->use(new TimestampBetween('daofang_time','到访时间'))->datetime();
            $filter->where(function ($query){
                switch ($this->input) {
                    case 1:
                        $query->where('baobei_time','>',0);
                        break;
                    case 2:
                        $query->where('baobei_time','=',null);
                        break;
                }
            },'报备状态','baobei')->select([3 => '全部',1=>'已报备',2=>'未报备']);
            $filter->use(new TimestampBetween('baobei_time','报备时间'))->datetime();
            $filter->use(new TimestampBetween('create_time','创建时间'))->datetime();
            $filter->equal('project_id','项目')->select(Project::getSelectOptions());
            $filter->equal('owner_user_id','负责人')->select(User::getSelectOptions(\Request::get('project_id','')));
            $filter->equal('create_user_id','创建人')->select(User::getSelectOptions(\Request::get('project_id','')));

        });
        $grid->batchActions(function ($batch) {
            $batch->disableDelete();
            $batch->add(new Transfer());
            $batch->add(new SeaAction());
        });
        $grid->exporter(new CustomerExcelExporter());
        return $grid
;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Customer::findOrFail($id));

        $show->field('customer_id', __('Customer id'));
        $show->field('name', __('Name'));
        $show->field('is_lock', __('Is lock'));
        $show->field('deal_status', __('Deal status'));
        $show->field('deal_time', __('Deal time'));
        $show->field('level', __('Level'));
        $show->field('industry', __('Industry'));
        $show->field('source', __('Source'));
        $show->field('telephone', __('Telephone'));
        $show->field('mobile', __('Mobile'));
        $show->field('website', __('Website'));
        $show->field('remark', __('Remark'));
        $show->field('create_user_id', __('Create user id'));
        $show->field('owner_user_id', __('Owner user id'));
        $show->field('ro_user_id', __('Ro user id'));
        $show->field('rw_user_id', __('Rw user id'));
        $show->field('address', __('Address'));
        $show->field('location', __('Location'));
        $show->field('detail_address', __('Detail address'));
        $show->field('lng', __('Lng'));
        $show->field('lat', __('Lat'));
        $show->field('next_time', __('Next time'));
        $show->field('follow', __('Follow'));
        $show->field('create_time', __('Create time'));
        $show->field('update_time', __('Update time'));
        $show->field('project_id', __('Project id'));
        $show->field('status', __('Status'));
        $show->field('clue_id', __('Clue id'));
        $show->field('crm_vfnlcv', __('Crm vfnlcv'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Customer());

        $form->text('name', __('Name'));
        $form->switch('is_lock', __('是否锁定'))->states($this->states);
        $form->text('deal_status', __('是否成交'))->default('未成交');
        $form->select('project_id', __('项目id'))->options(Project::getSelectOptions());
        $form->select('create_user_id', __('创建人员'))->options(User::getSelectOptions());
        $form->select('owner_user_id', __('负责人'))->options(User::getSelectOptions());
        return $form;
    }
}
